package custom;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.menhoo.menhoolibrary.R;

import java.util.List;

/**
 * Created by xiaolin on 2015/1/24.
 */
public abstract class GridViewAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> strList;
    private int hidePosition = AdapterView.INVALID_POSITION;

    public GridViewAdapter(Context context, List<T> strList) {
        this.context = context;
        this.strList = strList;
    }

    public int getHidePosition(){
        return hidePosition;
    }

    public List<T> getList(){
        return strList;
    }

    @Override
    public int getCount() {
        return strList.size();
    }

    @Override
    public T getItem(int position) {
        return strList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void hideView(int pos) {
        hidePosition = pos;
        notifyDataSetChanged();
    }

    public void showHideView() {
        hidePosition = AdapterView.INVALID_POSITION;
        notifyDataSetChanged();
    }

    public void removeView(int pos) {
        strList.remove(pos);
        notifyDataSetChanged();
    }

    //更新拖动时的gridView
    public void swapView(int draggedPos, int destPos) {
        //从前向后拖动，其他item依次前移
        if(draggedPos < destPos) {
            strList.add(destPos+1, getItem(draggedPos));
            strList.remove(draggedPos);
        }
        //从后向前拖动，其他item依次后移
        else if(draggedPos > destPos) {
            strList.add(destPos, getItem(draggedPos));
            strList.remove(draggedPos+1);
        }
        hidePosition = destPos;
        changed();
        notifyDataSetChanged();
    }

    public void changed(){}
}
